package tr.edu.metu.is550.isell.security.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import tr.edu.metu.is550.isell.domain.User;
import tr.edu.metu.is550.isell.repository.UserRepository;

@Component("isellUserDetailService")
public class ISellUserDetailsService implements UserDetailsService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUserCredentialsLoginName(username);
        if (user == null)
            throw new UsernameNotFoundException("User not found!");


        return new UserDetailsWrapper(user);
    }
}
